var app = getApp()
var url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
  },
  pageIndex: 1,
  pageSize: 100,
  navbarTap: function (e) {
    const that = this;
    const userId = app.globalData.userid
    console.log(userId)
    const idx = e.currentTarget.dataset.idx
    if(idx == 0){
      /**
       * 查询全部
       */
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userId,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          payStatus: 2,
          shippingStatus: 2
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == '没有查到订单数据信息') {
            that.setData({
              hide:false,
              currentTab: e.currentTarget.dataset.idx
            })
          } else { 
            that.setData({
              hide:true,
              order: res.data.object.orderList,
              currentTab: e.currentTarget.dataset.idx
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(idx == 1){
      /**
       * 查询商城
       */
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userId,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          orderType: 2,
          payStatus: 2,
          shippingStatus: 2
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == '没有查到订单数据信息') {
            that.setData({
              order: "",
              hide: false,
              currentTab: e.currentTarget.dataset.idx
            })
          } else {
            that.setData({
              hide: true,
              order: res.data.object.orderList,
              currentTab: e.currentTarget.dataset.idx
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(idx == 2){
      /**
       * 外卖订单
       */
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userId,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          orderType: 1,
          payStatus: 2,
          shippingStatus: 2
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == '没有查到订单数据信息') {
            that.setData({
              order: "",
              hide: false,
              currentTab: e.currentTarget.dataset.idx
            })
          } else {
            that.setData({
              hide: true,
              order: res.data.object.orderList,
              currentTab: e.currentTarget.dataset.idx
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  }, 
  detail:function(e){
    const orderId = e.currentTarget.dataset.orderid
    const orderType = e.currentTarget.dataset.ordertype
    if(orderType == 1){
      wx.navigateTo({
        url: '../order/detail/detail?orderId=' + orderId,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }else if(orderType == 2){
      wx.navigateTo({
        url: '../../orderDetail/orderDetail?orderId=' + orderId,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    const userId = app.globalData.userid
    /**
     * 查询全部订单
     */
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userId,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        payStatus: 2,
        shippingStatus: 2
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == '没有查到订单数据信息') {
        
        } else {
          that.setData({
            order: res.data.object.orderList
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
})